<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/DetailTemplate.master" Inherits="System.Web.Mvc.ViewPage<DotNetBurner.StorySubmitData>" %>
<%@ Import Namespace="DotNetBurner" %>
<%@ Import Namespace="System.Web.Mvc" %>
<%@ Import Namespace="Resources" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<%using (Html.BeginForm("Submit", "Story", FormMethod.Post, new { id = "submitform", onsubmit = "return storySubmit(this)" })) 
  {%>
    <%=Html.AntiForgeryToken("submit")%>
    <%=Html.Hidden("storyId", ViewData.Model.StoryId)%>
    <div class="tab">
        <h1><%=Strings.SubmitStory%></h1>
    </div>
    <table cellpadding="0" cellspacing="0" style="border: 0px">
    <tr>
        <td style="width: 650px" valign="top">
            <p>
                <label for="storyUrl" class="label"><%=Strings.Url%>:</label>
                <%=Html.TextBox("storyUrl", ViewData.Model.StoryUrl, new { @class = "largeTextbox", @id = "storyUrl", @onblur = "autoLoad()" })%>
            </p>
            <p>
                <label for="storyTitle" class="label"><%=Strings.Title%>:</label>
                <%=Html.TextBox("storyTitle", ViewData.Model.StoryTitle, new { @class = "largeTextbox", @id = "storyTitle" })%>
            </p>
            <p>
                <label for="storyCategoryId" class="label"><%=Strings.Category%>:</label>
                <select id="storyCategoryId" name="storyCategoryId" class="largeTextbox">
                    <option value="-1"><%=Strings.SelectCategory%></option>
                    <% foreach (var category in ViewData.Model.Categories.OrderBy(c => c.Name)) %>
                    <% { %>
                            <optgroup label="<%=category.Name%>">
                            <%  foreach (var subcat in category.SubCategories)
                                {%>
                                    <% string sel = (ViewData.Model.StoryCategoryId == subcat.CategoryId) ? " selected=\"selected\"" : ""; %>
                                    <option value="<%=subcat.CategoryId%>"<%=sel%>><%=subcat.Name.HtmlEncode()%></option>
                              <%} %>
                            </optgroup>                              
                    <% } %>
                </select>
            </p>
            <p>
                <label for="storyTags" class="label"><%=Strings.Tags%>:</label>
                <%=Html.TextBox("storyTags", ViewData.Model.StoryTags, new { @class = "largeTextbox", @id = "storyTags"})%>
            </p>
            <p>
                <label class="label"></label><span><%=Strings.SeparateTagsWith%></span>
            </p>
            <p>
                <label for="txtStoryDescription" class="label"><%=Strings.Description%>:</label>
                <%=Html.TextArea("storyDescription", ViewData.Model.StoryDescription, new { @cols = 20, @rows = 8, @class = "largeTextarea", @id = "storyDescription" })%>
            </p>
            <div id="submitMessages" class="messages"></div>
            <div id="submitErrors" class="errors"></div>
            <p>
                <label class="label"></label>
                <input type="submit" value="<%=Strings.SubmitStory%>" id = "btnSubmit" />
                <input type="reset" onclick="storyClear()" value="<%=Strings.Clear%>"  id = "btnReset" />
            </p>
            <%Html.RenderPartialLang("SubmitTips"); %>
        </td>
        <td valign="top">
           <%Html.RenderPartialLang("SubmitGuidelines");%>
        </td>
    </tr>        
</table>    
<%} %>
<script type="text/javascript">
    function storySubmitSuccess(res) {
        data = res.Data;
        window.open(data.url, '_self');
    }

    function storySubmit(form) {
        formSubmit(form, '#submitErrors', '', '#btnSubmit', storySubmitSuccess, '', '');

        return false;
    }

    function storyClear(form) {
        $('#submitMessages').empty();
        $('#submitErrors').empty();
    }

    function autoLoad() {
        if ($("#storyDescription").val() == '' && $("#storyUrl").val() != '') {
            storyClear();
            $('#submitMessages').append("<p><%=Strings.WaitWhilePopulating%></p>");
            $('#submitMessages').fadeIn(300);

            loadSubmitData();
        }
    }

    function loadSubmitData() {
        var url = $("#storyUrl").val();

        if (url != '') {
            $.ajax({
                url: '<%=Url.Content("~/GetSubmitData.axd")%>',
                type: 'GET',
                data: 'url=' + encodeURIComponent(url),
                dataType: 'json',
                success: function(res) {
                    data = res;
                    if ($("#storyTitle").val() == '')
                        $("#storyTitle").val(data.StoryTitle);
                    if ($("#storyDescription").val() == '')
                        $("#storyDescription").val(data.StoryDescription);
                    if ($("#storyTags").val() == '')
                        $("#storyTags").val(data.StoryTags);
                    if ($("#storyCategoryId").val() == -1)
                        $("#storyCategoryId").val(data.StoryCategoryId);
                },
                complete: function() {
                    $('#submitMessages').fadeOut(0);
                    $('#submitMessages').empty();
                    $('#submitMessages').append("<p><%=Strings.ReviewAutoPopulate%></p>");
                    $('#submitMessages').fadeIn(300);
                }
            });
        }
    }
    toptags = "<%=Model.AutoCompleteData%>".split('|');

    $(document).ready(function() {
        $("#storyTags").autocomplete(toptags, {
            multiple: true
        });

        autoLoad();

        $('#guidelines').fadeIn(1000);
        $('#tips').fadeIn(1000);
    });
</script>
<script type="text/javascript" src="<%=Html.Content("jquery.autocomplete.js", true)%>"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderPlaceHolder" runat="server">
    <link href="<%=Html.Content("jquery.autocomplete.css", true)%>" rel="stylesheet" type="text/css" />
</asp:Content>
